import { baseUrl } from "@/utils/baseUrl.js"
const req = ({
	url,
	data,
	showLoad = true,
	method = 'GET',
	header = {
		"Content-Type": "application/json;charset=UTF-8",
		"Authorization": uni.getStorageSync("token") && `Bearer ${uni.getStorageSync("token")}`
	}
}) => {
	if(showLoad) {
		uni.showLoading({
			title: "加载中...",
			mask: true
		})
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method,
			header,
			data,
			success: res => {
				if(url.includes('aichat')) {
					resolve(res.data)
				}
				const {code, data, msg} = res.data
				if (code >= 200 && code < 300) {
					resolve(data)
				} else {
					if (code == 401|| code == 403) {
						uni.removeStorageSync('token')
						setTimeout(() => uni.navigateTo({ url: "/pagesSubPack/myCenter/login" }), 1500)
					}
					reject(msg)
				}
			},
			complete: res => {
				uni.hideLoading({
					complete: () => {
						res.data.code !== 200 && uni.showToast({
							icon: 'none',
							title: res.data.msg,
							duration: 2000
						})
					}
				})
			},
			fail: err => reject(err)
		})
	})
}
export default req